import { registerDecorator, ValidationOptions } from 'class-validator';

export function IsMobilePhone(validationOptions?: ValidationOptions) {
  return function (object: object, propertyName: string) {
    registerDecorator({
      name: 'isMobilePhone',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: {
        validate(value: string) {
          const regex = /^1[3-9]\d{9}$/;
          return regex.test(value);
        },
        defaultMessage() {
          return '手机号码格式不正确';
        },
      },
    });
  };
}
